<?php
header("Content-Type: text/html; charset=iso-8859-1");
require('include.php');
if(!check_admin()) redirect('perso.php?action=admin');
else{
	$_SESSION['navigation'] = "documents_admin";
	$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : "nom";
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
	$fields = describe('documents');
	$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";	
	$message = "";
	if($action == 'list'){
		$table = new table();
		$fields['actions'] = "type";
		foreach($fields as $field => $type){
			if($field != 'id'){
				$$field= new table_field($field);
				$$field->print = $field;
				if($type == 'int(1)' || $field == 'actions') $$field->styleclass = 'center';
				$table->fields[] = $$field;
			}
		}
		$q = "select * from documents order by $order";
		$r = ExecRequete($q);
		$i = 0;
		while($s = LigneSuivante($r)){
			$row = new table_row();
			foreach($fields as $field => $type){
				if($field != 'id'){
					if($field == 'fichier'){
						$doctype = (ereg(".*\.([a-zA-Z]*)",$s->fichier,$regs)) ? strtolower($regs[1]) : "txt";
						$print = "<a href = 'documents/$s->fichier'><img src = 'images/icon_$doctype.png' alt = '$doctype' width = '14' height = '14' />&nbsp;$s->fichier</a>\n";
					}
					elseif($type == "int(1)"){
						$checked = ($s->$field) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
						$print =  "$checked\n";
					}
					elseif($field == 'actions'){
						$print =  "<img src = 'images/icon_edit.png' alt = 'edit' width = '14' height = '14' style = 'cursor: pointer' onclick = 'edit_document($s->id)' />";
						$print .=  "<img src = 'images/icon_delete.png' alt = 'del' width = '14' height = '14' style = 'cursor: pointer' onclick = 'if(confirm(\"Voulez-vous vraiment supprimer le fichier ?\"))del_document($s->id)' />\n";
					}
					elseif($field == 'droits'){
						$droits = array("m" => "membres","c" => "comit&eacute;");
						$print = (isset($droits[$s->droits])) ? $droits[$s->droits] : "tous";
					}
					else{
						$print =  $s->$field."\n";
					}
					$row->cols[$field] = $print;
				}
			}
			$table->rows[] = $row;
		}
		echo "<p class = 'center'><button type = 'button' onclick = 'new_document()' name = 'new'><img src = 'images/icon_new.png' alt = '' width = '14' height = '14' />nouveau document</button></p>\n";
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();

	}
	elseif($action == 'new'){
		$titre =  "Nouveau\n";
		$content =  "<form action = 'documents_admin.php' method = 'post' name = 'myform' onsubmit = 'return check(this)' enctype = 'multipart/form-data' >\n";
		foreach($fields as $field => $type){
			$content .= leftCol($field);
			if($field == 'id'){
				$col = 'nouveau';
			}
			elseif($field == 'fichier'){
				$col = "<input type = 'file' name = 'fichier' size = '10' />\n";
			}
			elseif($type == 'int(1)'){
				$col = "<input type = 'checkbox' checked = 'checked' name = '$field' value = '1' />";
			}
			elseif($field == 'droits'){
				$col = "<select name = '$field'><option value = ''>tous</option>\n<option value = 'm'>membres</option>\n<option value = 'c'>comit&eacute;</option>\n</select>\n";
			}
			else{
				$col = "<input type = 'text' name = '$field' size = '20' />\n"; 
			}
			$content .= rightCol($col); 
		}
		$content .= "<br class = 'clear'>\n";
		$content .= "<p class = 'center'><button type = 'submit' name = 'action' value = 'add'>ajouter un document</button>\n";
		$content .= "<button type = 'button' onclick = 'list_documents(\"$order\")'>annuler</button><input type = 'hidden' name = 'MAX_FILE_SIZE' value = '9999999' /></p>\n";
		$content .= "</form>\n";
		echo "<div class = 'center' style = 'margin-left: 60px'>\n";
		box($titre,$content,500,1);
		echo "</div>\n";
	}
	elseif($action == 'edit'){
		$fichier = new document($id);
		if($fichier->nom){
			$titre =  "Modifier $fichier->nom\n";
			$content =  "<form action = 'documents_admin.php' method = 'post' name = 'myform'  enctype = 'multipart/form-data' >\n";
			foreach($fields as $field => $type){
				$content .= leftCol($field);
				if($field == 'fichier'){
					$col = "<input type = 'file' name = 'fichier' size = '10' />\n";
				}
				elseif($type == 'int(1)'){
					$checked = ($fichier->valide) ? "checked = 'checked'" : "";
					$col = "<input type = 'checkbox'  name = '$field' value = '1' $checked/>";
				}
				elseif($field == 'droits'){
					$droits = array("" => "tous","m"=>"membres","c"=>"comit&eacute;");
					$col = "<select name = '$field'>";
					foreach($droits as $option => $droit){
						$selected = ($option == $fichier->droits) ? "selected = 'selected'" : "";
						$col .= "<option value = '$option' $selected>$droit</option>";
					}
					$col .= "</select>";
				}
				else{
					$col = "<input type = 'text' name = '$field' size = '20' value = '".$fichier->$field."' />\n"; 
				}
				$content .= rightCol($col); 
			}
			$content .= "<br class = 'clear'>\n";
			$content .= "<p class = 'center'><button type = 'submit' name = 'action' value = 'modif'>modifier</button>";
			$content .= "\n<button type = 'button' onclick = 'list_documents(\"$order\")'>annuler</button>\n<input type = 'hidden' name = 'MAX_FILE_SIZE' value = '9999999' />\n<input type = 'hidden' name = 'id' value = '$id' />\n";
			$content .= "\n</p>\n";
			$content .= "</form>\n";
			echo "<div class = 'center' style = 'margin-left: 60px'>\n";
			box($titre,$content,500,1);
			echo "</div>\n";
		}		
	}
	elseif($action == 'delete'){
		$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
		if($id){
			$fichier = new document($id);
			if($fichier->delete()) echo "Le document a &eacute;t&eacute; effac&eacute;";
			else echo "Désolé, une erreur est survenue. Le document n'a pas &eacute;t&eacute; effac&eacute;.";			
		} 
		else echo "Désolé, une erreur est survenue. Le document n'a pas &eacute;t&eacute; effac&eacute;.";
	}

	else{
		if($action == 'modif'){
			$fichier = new document($id);
			if(!empty($_FILES['fichier'])){
				$name = (!empty($_REQUEST['nom'])) ? $_REQUEST['nom'] : $_FILES['fichier']['name'];
				$doctype = (ereg(".*\.([a-zA-Z]*)",$_FILES['fichier']['name'],$regs)) ? strtolower($regs[1]) : "txt";			
				$filename = preg_replace("/\s+/","_",$name);
				$filename = preg_replace("/[^\w\d._\-]/","",$filename);
				if(ereg(".*\.([a-zA-Z]*)",$filename,$regs) === FALSE) $filename .= ".$doctype";
				if(move_uploaded_file($_FILES['fichier']['tmp_name'], "documents/$filename")){
					$fichier->fichier = $filename;
					chmod("documents/".$filename,0644);
				}
				else $message = "D&eacute;sol&eacute;, le fichier n'a pas pu &ecirc;tre charg&eacute; !";
			}
			else $message = "Aucun fichier n'a &eacute;t&eacute; fourni";
			foreach($fields as $field => $type){
				if($field != 'fichier')	$fichier->$field = (!empty($_REQUEST[$field])) ? $_REQUEST[$field] : (($field != 'valide') ? $fichier->$field : 0);
			}
			if($fichier->update()) $message = "Modifications enregistr&eacute;es";
			else $message = "Désolé, le fichier n'a pas &eacute;t&eacute; modifi&eacute;";
		}
		elseif($action == 'add'){
			if(!empty($_FILES['fichier'])){
				$name = (!empty($_REQUEST['nom'])) ? $_REQUEST['nom'] : $_FILES['fichier']['name'];
				$doctype = (ereg(".*\.([a-zA-Z]*)",$_FILES['fichier']['name'],$regs)) ? strtolower($regs[1]) : "txt";			
				$filename = preg_replace("/\s+/","_",$name);
				$filename = preg_replace("/[^\w\d._\-]/","",$filename);
				if(ereg(".*\.([a-zA-Z]*)",$filename,$regs) === FALSE) $filename .= ".$doctype";
				if(move_uploaded_file($_FILES['fichier']['tmp_name'], "documents/$filename")){
					chmod("documents/".$filename,0644);
					$fichier = new document;
					$fichier->nom = (ereg("(.*)\.([a-zA-Z]*)",$name,$regs)) ? $regs[1] : $name;
					$fichier->fichier = $filename;
					$fichier->valide = (!empty($_REQUEST['valide'])) ? $_REQUEST['valide'] : 0;
					$fichier->droits = (isset($_REQUEST['droits'])) ? $_REQUEST['droits'] : "";
					if($fichier->insert()) 	$message = "Fichier charg&eacute; avec succ&egrave;s";
					else $message = "D&eacute;sol&eacute;, le fichier n'a pas pu &ecirc;tre charg&eacute; !";
				}
				else $message = "D&eacute;sol&eacute;, le fichier n'a pas pu &ecirc;tre charg&eacute; !";
			}
			else $message = "Aucun fichier n'a &eacute;t&eacute; fourni";
		}
		$script = "function check(myform){\n";
		$script .= "	if(document.myform.fichier.value == ''){\n";
		$script .= "		alert('Vous n\'avez pas chargé de fichier !!');\n";
		$script .= "		document.myform.fichier.focus();\n";
		$script .= "	}\n";
		$script .= "}\n";
		entete("VBCLL Documents",$script,"",1);
		if($message) message($message);
		echo "<h1>Documents</h1>\n";
		echo "<div id = 'action'></div>\n";
		echo "<script type = 'text/javascript'>list_documents('$order')</script>\n";
		pied();
	}	
}
?>